from playwright.sync_api import Playwright, sync_playwright
import random
import time
from fake_useragent import UserAgent
# List of user agents
user_agents = UserAgent()


def set_random_user_agent(context):
    user_agent = user_agents.random
    context.set_extra_http_headers({"User-Agent": user_agent})


def login_58(page):
    set_random_user_agent(page.context)
    page.goto("https://passport.58.com/login/?path=https%3A//www.58.com/ershoujiaju/")
    page.get_by_text("短信登录").click()
    page.get_by_placeholder("请输入手机号").click()
    page.get_by_placeholder("请输入手机号").fill("请输入你的电话号码")
    page.get_by_text("获取验证码").click()
    time.sleep(1)


def login_jinrong(page):
    set_random_user_agent(page.context)
    page.goto("http://jrh.financeun.com/Login/jrwLogin?web=jrw")
    page.get_by_text("短信快捷登录").click()
    page.get_by_role("textbox", name="请输入手机号").click()
    page.get_by_role("textbox", name="请输入手机号").fill("请输入你的电话号码")
    page.get_by_text("获取验证码").click()
    time.sleep(1)


def login_wenxinyiyan(page):
    set_random_user_agent(page.context)
    page.goto("https://yiyan.baidu.com/?utm_source=itab")
    page.get_by_text("立即登录").click()
    page.get_by_text("短信登录").click()
    page.get_by_placeholder("请输入手机号").click()
    page.get_by_placeholder("请输入手机号").fill("请输入你的电话号码")
    page.get_by_role("button", name="发送验证码").click()
    time.sleep(1)


def login_baidu(page):
    set_random_user_agent(page.context)
    page.goto("https://www.baidu.com/")
    page.get_by_role("link", name="登录").click()
    page.get_by_text("短信登录").click()
    page.get_by_placeholder("请输入手机号").click()
    page.get_by_placeholder("请输入手机号").fill("请输入你的电话号码")
    page.get_by_role("button", name="发送验证码").click()
    time.sleep(50)
    print("开始延迟50秒")


def execute_logins(playwright: Playwright, iterations: int):
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()

    for _ in range(iterations):
        page = context.new_page()
        login_58(page)
        login_wenxinyiyan(page)
        login_jinrong(page)
        login_baidu(page)

    context.close()
    browser.close()


with sync_playwright() as playwright:
    execute_logins(playwright, 1)  # Replace 1 with the desired number of iterations
